package xyz.jcat.common.web;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import xyz.jcat.common.BaseBean;

@Getter
@Setter
@NoArgsConstructor
public class Resp<T> extends BaseBean {

    //响应码
    private int code;
    //响应说明
    private String msg;
    //响应数据
    private T data;

    public static Resp ok() {
        return new Resp(RespCodeMsg.OK);
    }

    public static <T> Resp<T> ok(T data) {
        return new Resp(RespCodeMsg.OK, data);
    }

    public static Resp fail(IRespCodeMsg respCodeMsg) {
        return new Resp(respCodeMsg);
    }

    public static Resp fail(IRespCodeMsg respCodeMsg, String errorMsg) {
        return new Resp(respCodeMsg.getCode(), errorMsg);
    }

    public static Resp fail(String msg) {
        return new Resp(RespCodeMsg.SERVICE_ERROR.getCode(), msg);
    }

    public static <T> Resp<T> fail(IRespCodeMsg respCodeMsg, T data) {
        return new Resp<>(respCodeMsg, data);
    }

    @JsonIgnore
    public boolean isOk() {
        return this.code == RespCodeMsg.OK.getCode();
    }

    public Resp(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Resp(IRespCodeMsg codeMsg) {
        this(codeMsg.getCode(), codeMsg.getMsg());
    }

    public Resp(IRespCodeMsg codeMsg, T data) {
        this(codeMsg);
        this.data = data;
    }

}
